package cn.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.dao.CompanyDao;
import cn.dao.StudentDao;
import cn.dao.TeacherDao;
import cn.dao.UserDao;
import cn.entity.TCompany;
import cn.entity.TStudent;
import cn.entity.TTeacher;
import cn.entity.TUser;
import cn.util.PageBean;
@Controller("userAction")
@Scope("prototype")
public class UserAction extends BaseAction{
	private Integer id;
	private String username;
	private String password;
	private String password1;
	private Integer role;

	@Autowired
	@Qualifier("userDao")
	private UserDao userDao;
	
	@Autowired
	@Qualifier("studentDao")
	private StudentDao studentDao;
	
	@Autowired
	@Qualifier("teacherDao")
	private TeacherDao teacherDao;
	
	@Autowired
	@Qualifier("companyDao")
	private CompanyDao companyDao;

	//登录
	public String login(){
		String hql="";
		session.setAttribute("role", role);
		if(role==1){//毕业生
			hql="from TStudent where username=? and password=?";
			List<TStudent> list=studentDao.getByHql(hql,username,password);
			if(list!=null && list.size()>0){
				TStudent student=list.get(0);
				session.setAttribute("currentUser", student);
				return "index";
			}
		}else if(role==2){//管理员
			hql="from TUser u where username=? and password=?";
			List<TUser> list=userDao.getByHql(hql,username,password);
			if(list!=null && list.size()>0){
				TUser user=list.get(0);
				session.setAttribute("currentUser", user);
				return "index";
			}
		}else if(role==3){//辅导员
			hql="from TTeacher where username=? and password=?";
			List<TTeacher> list=teacherDao.getByHql(hql,username,password);
			if(list!=null && list.size()>0){
				TTeacher teacher=list.get(0);
				session.setAttribute("currentUser", teacher);
				return "index";
			}
		}
//		else if(role==4){//用人单位
//			hql="from TCompany where username=? and password=?";
//			List<TCompany> list=companyDao.getByHql(hql,username,password);
//			if(list!=null && list.size()>0){
//				TCompany company=list.get(0);
//				session.setAttribute("currentUser", company);
//				return "index";
//			}
//		}

		request.setAttribute("msg", "登录失败!");
		return "login";
		
	}
	public String updatepwd(){
		Integer role=(Integer)session.getAttribute("role");
		if(role==1){//毕业生
			TStudent student=(TStudent)session.getAttribute("currentUser");
			if(!password.equals(student.getPassword())){
				request.setAttribute("msg", "原密码不正确!");
				return "updatepwd";
			}else{
				student.setPassword(password1);
				studentDao.update(student);
			}
		}else if(role==2){//管理员
			TUser user=(TUser)session.getAttribute("currentUser");
			if(!password.equals(user.getPassword())){
				request.setAttribute("msg", "原密码不正确!");
				return "updatepwd";
			}else{
				user.setPassword(password1);
				userDao.update(user);
			}
		}else if(role==3){//辅导员
			TTeacher teacher=(TTeacher)session.getAttribute("currentUser");
			if(!password.equals(teacher.getPassword())){
				request.setAttribute("msg", "原密码不正确!");
				return "updatepwd";
			}else{
				teacher.setPassword(password1);
				teacherDao.update(teacher);
			}
		}
		request.setAttribute("msg", "密码修改成功,请重新登录!");
		return "login";
	}
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}


	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getRole() {
		return role;
	}

	public void setRole(Integer role) {
		this.role = role;
	}



	public String getPassword1() {
		return password1;
	}



	public void setPassword1(String password1) {
		this.password1 = password1;
	}


}
